'Copyright 2006 James Downey

Public Class Entity

    '  <!--
    '  entity element - used for specifying the root element for a fetch, only
    '           one root entity is allowed in a given fetch, all others
    '           are dependent on this entity and are marked as
    '           linked-entity
    '-->
    '<ElementType name='entity' content='eltOnly' model='closed' order='many'>
    '  <AttributeType name='name' dt:type='string' required='yes' />
    '  <!-- -->

    '  <attribute type='name' />
    '  <element type='all-attributes' minOccurs='0' maxOccurs='1' />
    '  <element type='attribute' minOccurs='0' maxOccurs='*' />
    '  <element type='order' minOccurs='0' maxOccurs='*' />
    '  <element type='linked-entity' />
    '  <element type='filter' minOccurs='0' maxOccurs='1' />
    '</ElementType>

    Protected mName As String
    Public AllAttributes As Boolean = False
    Public NoAttributes As Boolean = False
    Public mAttributes As Attributes
    Public mOrders As Orders
    Public mLinkedEntities As LinkedEntities
    Public mFilters As Filters

    Public ReadOnly Property Name() As String
        'this is readonly because you cannot change the name of 
        'the element without all of the other attributes becoming invalid
        Get
            Return mName
        End Get
    End Property

    Public Sub New(ByVal EntityName As String)
        Me.mName = EntityName
        mAttributes = New Attributes
        mOrders = New Orders()
        mLinkedEntities = New LinkedEntities
        mFilters = New Filters
    End Sub

    Public Sub New(ByVal EntityNode As Xml.XmlNode)

        'Dim doc As New Xml.XmlDocument
        'doc.LoadXml(EntityNode.OuterXml)
        'EntityNode = doc.DocumentElement

        Me.New(EntityNode.Attributes("name").Value)

        If Not EntityNode.SelectSingleNode("./all-attributes") Is Nothing Then
            Me.AllAttributes = True
        End If

        If Not EntityNode.SelectSingleNode("./no-attributes") Is Nothing Then
            Me.NoAttributes = True
        End If

        Dim attribnodes, ordernodes, linkedentitynodes, filternodes As Xml.XmlNodeList
        Dim attribnode, ordernode, linkedentitynode, filternode As Xml.XmlNode

        attribnodes = EntityNode.SelectNodes("./attribute")
        For Each attribnode In attribnodes
            Me.Attributes.Add(New Attribute(attribnode))
        Next

        ordernodes = EntityNode.SelectNodes("./order")
        For Each ordernode In ordernodes
            Me.Orders.Add(New Order(ordernode))
        Next

        linkedentitynodes = EntityNode.SelectNodes("./link-entity")
        For Each linkedentitynode In linkedentitynodes
            Me.LinkedEntities.Add(New LinkedEntity(linkedentitynode))
        Next

        filternodes = EntityNode.SelectNodes("./filter")
        For Each filternode In filternodes
            Me.Filters.Add(New Filter(filternode))
        Next


    End Sub

    Public ReadOnly Property Attributes() As Attributes
        Get
            Return mAttributes
        End Get
    End Property

    Public ReadOnly Property Orders() As Orders
        Get
            Return mOrders
        End Get
    End Property

    Public ReadOnly Property Filters() As Filters
        Get
            Return mFilters
        End Get
    End Property

    Public ReadOnly Property LinkedEntities() As LinkedEntities
        Get
            Return mLinkedEntities
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return ToXML()
    End Function

    Public Function ToXML() As String
        Dim strXML As String
        strXML = "<entity name='" & Name & "'>"
        If AllAttributes Then
            strXML &= "<all-attributes />"
        End If
        If Me.NoAttributes Then
            strXML &= "<no-attrs />"
        End If
        'If Not Me.AllAttributes And Me.Attributes.Count = 0 Then
        '    strXML &= "<no-attributes />"
        'End If
        If Not AllAttributes And Not NoAttributes Then
            Dim attribute As Attribute
            For Each attribute In Me.Attributes
                strXML &= attribute.ToXML
            Next
        End If
        
        Dim order As Order
        For Each order In Me.Orders
            strXML &= order.ToXML
        Next
        Dim filter As Filter
        For Each filter In Me.Filters
            strXML &= filter.ToXML
        Next
        Dim link As LinkedEntity
        For Each link In Me.LinkedEntities
            strXML &= link.ToXML
        Next
        strXML &= "</entity>"
        Return strXML
    End Function

End Class
